package com.jyx.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.jyx.project.enums.ProjectImageTypeEnume;
import com.jyx.project.mapper.*;
import com.jyx.project.pojo.*;
import com.jyx.project.service.ProjectCreateService;
import com.jyx.project.vo.req.ProjectStoreToRedisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectCreateServiceImpl implements ProjectCreateService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired(required = false)
    private TProjectMapper projectMapper;

    @Autowired(required = false)
    private TProjectImagesMapper imagesMapper;

    @Autowired(required = false)
    private TProjectTypeMapper typeMapper;

    @Autowired(required = false)
    private TProjectTagMapper tagMapper;

    @Autowired(required = false)
    private TReturnMapper returnMapper;

    @Override
    //初始化项目,返回项目令牌
    public String initCreateProject(Integer memberId) {
        //创建初始化对象
        ProjectStoreToRedisVo toRedisVo = new ProjectStoreToRedisVo();
        //将用户id存入初始化对象
        toRedisVo.setMemberid(memberId);
        //创建项目令牌
        String projectToken = "Project_" + UUID.randomUUID().toString().replace("-","");
        //存储项目令牌
        toRedisVo.setProjectToken(projectToken);
        //将初始化对象转为JSON字符串
        String jsonString = JSON.toJSONString(toRedisVo);
        //将初始化对象存入redis,    键:项目令牌,值:初始化对象
        redisTemplate.opsForValue().set(projectToken,jsonString);
        return projectToken;
    }

    @Override
    //将项目信息存储到数据库
    public void saveProjectInfo(ProjectStoreToRedisVo toRedisVo) {
        TProject project = new TProject();
        BeanUtils.copyProperties(toRedisVo,project);
        // 创建时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(new Date());
        project.setCreatedate(date);
        // 将project数据插入数据库
        projectMapper.insert(project);
        // 获取刚插入数据的project的ID
        Integer projectId = project.getId();

        // 头图片
        if (toRedisVo.getHeadImage() != null) {
            TProjectImages headerImages = new TProjectImages(projectId, toRedisVo.getHeadImage(), ProjectImageTypeEnume.HEADER.getCode());
            imagesMapper.insert(headerImages);
        }
        // 详情图
        List<String> detailsImages = toRedisVo.getDetailsImages();
        if(detailsImages != null) {
            for (String detail : detailsImages) {
                TProjectImages detailImage = new TProjectImages(projectId, detail, ProjectImageTypeEnume.DETAILS.getCode());
                imagesMapper.insert(detailImage);
            }
        }
        // 标签
        List<Integer> tagIds = toRedisVo.getTagIds();
        for(Integer tagId : tagIds){
            TProjectTag tag = new TProjectTag(projectId, tagId);
            tagMapper.insert(tag);
        }
        // 类型
        List<Integer> typeIds = toRedisVo.getTypeIds();
        for(Integer typeId : typeIds){
            TProjectType type = new TProjectType(projectId, typeId);
            typeMapper.insert(type);
        }
        // 回报
        List<TReturn> returns = toRedisVo.getProjectReturns();
        for(TReturn tReturn : returns){
            tReturn.setProjectid(projectId);
            returnMapper.insert(tReturn);
        }

        // 删除redis
        redisTemplate.delete(toRedisVo.getProjectToken());
    }

}
